// pages/classic/classic.js


import ClassicModel from '../../models/classic.js';
const classicModel = new ClassicModel();

import LikeModel from '../../models/like.js';
const likeModel = new LikeModel();

Page({

    /**
    * 页面的初始数据
    */
    data: {
        first: false,
        latest: true,   //最后一期期刊
        likeStatus: false,  //点赞状态，默认不点赞
        likeCount: 0    //点赞数量
    },

    /**
    * 生命周期函数--监听页面加载
    */
    onLoad: function (options) {
        classicModel.getLatest((res) => {   
            this.setData({
                classic: res,
                likeStatus: res.like_status,
                likeCount: res.fav_nums
            });
        });
    },

    onLike(e){
        let behavior = e.detail.behavior;
        likeModel.like(behavior, this.data.classic.id, this.data.classic.type, (res) => {
            wx.showToast({
                title: behavior === 'like' ? '点赞成功' : '取消点赞',
                duration: 1000,
                icon: 'none'
            })
        });
    },

    onPrevOrNext(e){
        let behavior = e.detail.behavior;
        let index = this.data.classic.index;
        classicModel.getPrevOrNext(index, behavior, (res) => {
            this._getLikeInfo(res.type, res.id);
            this.setData({
                classic: res,
                first: classicModel.isFirst(res.index),
                latest: classicModel.isLatest(res.index)
            });
        });
    },
    _getLikeInfo(category, artId){
        likeModel.getLikeInfo(category, artId, (res) => {
            this.setData({
                likeStatus: res.like_status,
                likeCount: res.fav_nums
            });
        });
    },

    /**
    * 生命周期函数--监听页面初次渲染完成
    */
    onReady: function () {

    },

    /**
    * 生命周期函数--监听页面显示
    */
    onShow: function () {

    },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})